<template>
	<view class="content">
		<view class="z-index: 500;width: 750rpx;height: 504rpx;">
			<image src="../../static/img/ic_home_class_bj.png" style="width: 750rpx;height: 504rpx;z-index: 500;"
				mode=""></image>
		</view>
		<view class="px-2 position-relative" style="top: -140rpx;z-index: 600;">
			<class-item :list="list" @changeHead="getUserInfo" v-if="intType==2"></class-item>
			<teach-item :list="list" v-else></teach-item>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex';
	import classItem from '../../components/common/class-item.vue';
	import teachItem from '../../components/common/teach-item.vue';
	export default {
		data() {
			return {
				intType: 2,
				list: [],
			};
		},
		onShow() {
			this.intType = uni.getStorageSync('intType');
			if (this.intType == 2) {
				this.getUserInfo();
			} else {
				this.getteacherInfo()
				// this.list = uni.getStorageSync('userInfo').class_info
			}
		},
		components: {
			classItem,
			teachItem
		},
		// onPullDownRefresh() {
		// 	this.getUserInfo();
		// },
		methods: {
			getteacherInfo() {
				let _this = this;
				_this.list = []
				let arr = uni.getStorageSync('userInfo').class_info;
				arr.forEach((item, index) => {
					let opts = {
						url: 'SearchBj.shtml'
					};
					let param = {
						token: uni.getStorageSync('userInfo').token,
						class_id: item.class_id
					};
					_this.$http.httpRequest(opts, param).then(
						res => {
							console.log(res.data);
							if (res.data.code == '200') {
								item.school = res.data.data.school;
								_this.list.push(item)
							}
						})
				})
			},
			getUserInfo() {
				let _this = this;
				_this.list = [];
				let opts = {
					url: 'UserInfo.shtml'
				};
				let param = {
					token: uni.getStorageSync('userInfo').token
				};
				_this.$http.httpRequest(opts, param).then(
					res => {
						console.log(res.data);
						if (res.data.code == '200') {
							// uni.stopPullDownRefresh(); //停止下拉刷新动画
							let arr = res.data.data.class_info;
							_this.login(res.data.data);
							arr.forEach((item, index) => {
								let opts = {
									url: 'SearchBj.shtml'
								};
								let param = {
									token: uni.getStorageSync('userInfo').token,
									class_id: item.class_id
								};
								_this.$http.httpRequest(opts, param).then(
									res => {
										console.log(res.data);
										if (res.data.code == '200') {
											item.school = res.data.data.school;
											let opts = {
												url: 'device/elec?serialNo=' + item.imei +
													'&AuthToken=' + uni
													.getStorageSync('userInfo').token
											};
											let param = {
												serialNo: item.imei,
												AuthToken: uni.getStorageSync('userInfo').token
											};
											_this.$http.httpDeveiceRequest(opts, param).then(
												res => {
													if (res.data.ReturnMsg == "success") {
														item.dl = res.data.ReturnData.message
														let opts = {
															url: 'device/devicestatus?serialno=' +
																item.imei,
														};
														let param = {
															serialno: item.imei
														};
														_this.$http.httpDeveiceRequest(opts, param)
															.then(
																res => {
																	console.log(res.data.ReturnData
																		.deviceStatus);
																	if (res.data.ReturnMsg ==
																		"success") {
																		let status = res.data
																			.ReturnData.deviceStatus;
																		if (status == "ONLINE") {
																			item.sbstatus = "在线";
																			// _this.list.push(item);
																		} else {
																			item.sbstatus = "离线";
																			// _this.list.push(item);
																		}
																	} else {
																		item.sbstatus = "离线";
																		// _this.list.push(item);
																	}
																	let opts = {
																		url: 'sport/step?imei=' +
																			item.imei,
																	};
																	let param = {
																		imei: item.imei
																	};
																	_this.$http.httpDeveiceRequest(
																		opts, param).then(
																		res => {
																			console.log(res.data);
																			item.num = res.data
																				.ReturnData.Steps;
																			_this.list.push(item);
																		},
																		error => {
																			console.log(error);
																		}
																	);
																},
																error => {
																	console.log(error);
																}
															);
													} else {
														item.dl = "无电量",
															item.sbstatus = "无状态";
														item.num = "0";
														_this.list.push(item);
													}
												},
												error => {
													console.log(error);
												}
											);
										} else {
											uni.showToast({
												icon: 'none',
												title: res.data.msg
											});
										}
									},
									error => {
										console.log(error);
									}
								);
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
						}
					},
					error => {
						console.log(error);
					}
				);
			},
			...mapMutations(['login'])
		}
	};
</script>

<style></style>
